/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tedu.mall.sso.config;

import cn.tedu.mall.common.utils.JwtTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private JwtTokenUtils jwtTokenUtils;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("Bearer")
                .allowCredentials(true)
                .maxAge(3600);
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//      registry.addInterceptor(new HandlerInterceptor() {
//          @Override
//          public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//              System.out.println("访问进来了" +
//                      "");
//              String authorization = request.getHeader("Authorization");
//              System.out.println(authorization+"6e65cfd9-253e-48c6-b567-1974a700a35d");
//              if (ObjectUtils.isEmpty(authorization)){
//                  return   false;
//              }
//              String tocken = authorization.substring(7);
//              System.out.println(tocken+"token");
//              if (ObjectUtils.isEmpty(tocken)){
//                  return   false;
//              }
//              CsmallAuthenticationInfo  userInfo=null;
//              try {
//                  userInfo = jwtTokenUtils.getUserInfo(tocken);
//              }catch (Exception  e){
//                  return   false;
//              }
//              request.setAttribute("authorities",userInfo.getAuthorities());
//              return true;
//          }
//
//          @Override
//          public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//              HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
//          }
//
//          @Override
//          public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//              HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
//          }
//      }).addPathPatterns("/**").excludePathPatterns("/login/**");
//    }
}
